<?php
// Version: 1.3; Groupmods_install

// Create the groupmods table
db_query("
	CREATE TABLE IF NOT EXISTS {$db_prefix}groupmods (
	`ID_GROUP` smallint(5) unsigned NOT NULL default '0',
	`ID_MEMBER` mediumint(8) unsigned NOT NULL default '0',
	PRIMARY KEY ( `ID_GROUP` , `ID_MEMBER` )
	)", __FILE__, __LINE__);

// Add the GroupModOptions field to the membergroups table
$request = db_query("
	SHOW COLUMNS
	FROM {$db_prefix}membergroups
	LIKE 'GroupModOptions'", __FILE__, __LINE__);
$no_upgrade = mysql_num_rows($request) > 0;
if (!$no_upgrade) db_query("
	ALTER TABLE {$db_prefix}membergroups
	ADD `GroupModOptions` VARCHAR( 8 ) NOT NULL", __FILE__, __LINE__);
mysql_free_result($request);

// Add default permissions
// Initialize the groups array with 'ungrouped members' (0) and guests (-1).
$groups = array(-1, 0);
// Get all the non-postcount based groups.
$request = db_query("
	SELECT ID_GROUP
	FROM {$db_prefix}membergroups
	WHERE minPosts = -1", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
	$groups[] = $row['ID_GROUP'];
// Give them all their new permission.
$request = db_query("
	INSERT IGNORE INTO {$db_prefix}permissions
		(permission, ID_GROUP, addDeny)
	VALUES
		('view_groups', " . implode(", 1),
		('view_groups', ", $groups) . ", 1)", __FILE__, __LINE__);

?>